//
//  NSString+Nemo.m
//  NemoSDKDemo
//
//  Created by 许宝吉 on 2018/4/24.
//  Copyright © 2018年 JackYang. All rights reserved.
//

#import "NSString+Nemo.h"

@implementation NSString (Nemo)

- (NSString *)nemo_convertToJsonData:(id)obj
{
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
//        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}


+ (BOOL)nemo_isEmptyString:(NSString *)string
{
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

+ (NSString *)nemo_stringWithDuration:(NSUInteger)duration{
    long hour =  duration / 3600;
    long minute = (duration % 3600)/60;
    long second = duration % 60;
    NSString *timeString = nil;
    timeString = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hour, minute, second];
    return timeString;
}

- (NSString *)nemo_trim{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

- (NSMutableAttributedString *(^)(NSString *, CGFloat))nemo_appendFont{
    return ^(NSString *string, CGFloat font){
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n", self]];
        [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:font]}]];
        return attributedString;
    };
}

@end
